package com.service;

import com.entity.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author XiaoHao
 * @date 2021-03-22 08:52
 */
@Service
public class ImportTable {
    private ComponentService componentService;

    public ImportTable(@Autowired ComponentService componentService) {
        this.componentService = componentService;
    }

    public void importTable(Map<String, String> stringMap, Component old, Component newT) {

//        String newTableName = "work_time";
//        String oldTableName = "Emp";

        Set<Map.Entry<String, String>> entries1 = stringMap.entrySet();


        //获取老数据库的值
        //拿到老数据单表的所有数据
        List<Map<Object, Object>> select = componentService.select(old);

        for (Map<Object, Object> map : select) {
            //拿到每条数据
            Set<Map.Entry<Object, Object>> entries = map.entrySet();
            //准备存入新数据库
            Map<Object, Object> newMap = new TreeMap<>();
            //拿到每个字段
            for (Map.Entry<Object, Object> entry : entries) {
                //遍历页面传来的数据
                for (Map.Entry<String, String> stringEntry : entries1) {
                    //判断老字段是否一样  判断新字段是否一样
                    if (stringEntry.getKey().equalsIgnoreCase(entry.getKey() + "") && !"*".equals(entry.getValue())) {
                        //页面传递来的 value 即使新数据库的字段   , 老数据value是值
                        //把新数据的字段和老数据的值存到map中
                        newMap.put(stringEntry.getValue(), entry.getValue());

                    }
                }

            }
            //数据插入到新系统中
            if (newT != null && newT.getTableName() != null && newT.getTableName() != "" && newMap.size() >0 && newT != null) {
                newT.setParam(newMap);
                componentService.insert(newT);
            }

        }

    }


}
